package com.xbongbong.paas.help;

import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.help.subform.DistributorAccountTransferSubForm;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.domain.entity.DistributorAccountEntity;
import com.xbongbong.saas.domain.entity.OnlinePaymentFlowEntity;
import com.xbongbong.saas.domain.entity.ext.CustomerEntityExt;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.DistributorAccountEnum;
import com.xbongbong.saas.enums.business.DistributorEnum;
import com.xbongbong.saas.model.AssembleModel;
import com.xbongbong.saas.model.BomBillModel;
import com.xbongbong.saas.model.ClueModel;
import com.xbongbong.saas.model.CommunicatePlanModel;
import com.xbongbong.saas.model.CompetitorModel;
import com.xbongbong.saas.model.ContactModel;
import com.xbongbong.saas.model.ContractModel;
import com.xbongbong.saas.model.CostAdjustModel;
import com.xbongbong.saas.model.CreditCardBillModel;
import com.xbongbong.saas.model.CreditFixedLimitModel;
import com.xbongbong.saas.model.CreditLimitModel;
import com.xbongbong.saas.model.CreditTemporaryLimitModel;
import com.xbongbong.saas.model.CustomerCommunicateModel;
import com.xbongbong.saas.model.CustomerModel;
import com.xbongbong.saas.model.CustomerStatementStatisticsModel;
import com.xbongbong.saas.model.DistributorAccountModel;
import com.xbongbong.saas.model.ExpenseModel;
import com.xbongbong.saas.model.FundTransferModel;
import com.xbongbong.saas.model.InstockModel;
import com.xbongbong.saas.model.InventoryModel;
import com.xbongbong.saas.model.InvoiceModel;
import com.xbongbong.saas.model.MarketActivityModel;
import com.xbongbong.saas.model.OnlinePaymentFlowModel;
import com.xbongbong.saas.model.OpportunityModel;
import com.xbongbong.saas.model.OtherExpenseModel;
import com.xbongbong.saas.model.OtherIncomeModel;
import com.xbongbong.saas.model.OutstockModel;
import com.xbongbong.saas.model.PaasFormDataModel;
import com.xbongbong.saas.model.PayPlanModel;
import com.xbongbong.saas.model.PayPlanSheetModel;
import com.xbongbong.saas.model.PaymentModel;
import com.xbongbong.saas.model.PaymentSheetModel;
import com.xbongbong.saas.model.PaymentTaskModel;
import com.xbongbong.saas.model.ProductModel;
import com.xbongbong.saas.model.ProductionOrderModel;
import com.xbongbong.saas.model.PurchaseInvoiceModel;
import com.xbongbong.saas.model.PurchaseModel;
import com.xbongbong.saas.model.QuotationModel;
import com.xbongbong.saas.model.RefundModel;
import com.xbongbong.saas.model.ReimbursementFormModel;
import com.xbongbong.saas.model.ReturnedPurchaseModel;
import com.xbongbong.saas.model.SupplierCommunicateModel;
import com.xbongbong.saas.model.SupplierContactModel;
import com.xbongbong.saas.model.SupplierModel;
import com.xbongbong.saas.model.TransferModel;
import com.xbongbong.saas.model.WarehouseModel;
import com.xbongbong.saas.model.WorkOrderModel;
import com.xbongbong.saas.model.WorkReportModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/9/26 15:05
 * @since v1.0
 */
@Component
public class ProGetHelp {

    @Resource
    private PaasFormDataModel paasFormDataModel;
    @Resource
    private CustomerModel customerModel;
    @Resource
    private ContactModel contactModel;
    @Resource
    private CustomerCommunicateModel customerCommunicateModel;
    @Resource
    private CommunicatePlanModel communicatePlanModel;
    @Resource
    private OpportunityModel opportunityModel;
    @Resource
    private ContractModel contractModel;
    @Resource
    private RefundModel refundModel;
    @Resource
    private QuotationModel quotationModel;
    @Resource
    private WorkReportModel workReportModel;
    @Resource
    private SupplierModel supplierModel;
    @Resource
    private SupplierContactModel supplierContactModel;
    @Resource
    private SupplierCommunicateModel supplierCommunicateModel;
    @Resource
    private PurchaseModel purchaseModel;
    @Resource
    private ReturnedPurchaseModel returnedPurchaseModel;
    @Resource
    private BomBillModel bomBillModel;
    @Resource
    private ProductionOrderModel productionOrderModel;
    @Resource
    private AssembleModel assembleModel;
    @Resource
    private InstockModel instockModel;
    @Resource
    private OutstockModel outstockModel;
    @Resource
    private InventoryModel inventoryModel;
    @Resource
    private TransferModel transferModel;
    @Resource
    private WarehouseModel warehouseModel;
    @Resource
    private PaymentModel paymentModel;
    @Resource
    private PayPlanModel payPlanModel;
    @Resource
    private InvoiceModel invoiceModel;
    @Resource
    private WorkOrderModel workOrderModel;
    @Resource
    private PaymentSheetModel paymentSheetModel;
    @Resource
    private PayPlanSheetModel payPlanSheetModel;
    @Resource
    private PurchaseInvoiceModel purchaseInvoiceModel;
    @Resource
    private ProductModel productModel;
    @Resource
    private ReimbursementFormModel reimbursementFormModel;
    @Resource
    private ExpenseModel expenseModel;
    @Resource
    private ClueModel clueModel;
    @Resource
    private MarketActivityModel marketActivityModel;
    @Resource
    private PaymentTaskModel paymentTaskModel;
    @Resource
    private CustomerStatementStatisticsModel customerStatementStatisticsModel;
    @Resource
    private CreditLimitModel creditLimitModel;
    @Resource
    private CreditFixedLimitModel creditFixedLimitModel;
    @Resource
    private CreditTemporaryLimitModel creditTemporaryLimitModel;
    @Resource
    private CreditCardBillModel creditCardBillModel;
    @Resource
    private DistributorAccountModel distributorAccountModel;
    @Resource
    private DistributorAccountTransferSubForm distributorAccountTransferSubForm;
    @Resource
    private OnlinePaymentFlowModel onlinePaymentFlowModel;
    @Resource
    private FundTransferModel fundTransferModel;
    @Resource
    private OtherIncomeModel otherIncomeModel;
    @Resource
    private OtherExpenseModel otherExpenseModel;
    @Resource
    private CostAdjustModel costAdjustModel;
    @Resource
    private CompetitorModel competitorModel;

    /**
     * 获取单条数据
     * @param dataId
     * @param businessType
     * @param saasMark
     * @param corpid
     * @return
     * @throws XbbException
     */
    public PaasFormDataEntityExt getByKey(Long dataId, Integer businessType, Integer saasMark, String corpid) throws XbbException {
        PaasFormDataEntityExt paasFormDataEntityExt = new PaasFormDataEntityExt();
        if (Objects.equals(SaasMarkEnum.SAAS.getCode(), saasMark)) {
            XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);
            switch (xbbRefTypeEnum) {
                // CRM
                case CUSTOMER_MANAGEMENT:
                case DISTRIBUTOR_MANAGEMENT:
                    // 客户
                    paasFormDataEntityExt = customerModel.getByKey(dataId, corpid);
                    break;
                case CONTACT:
                case DISTRIBUTOR_CONTACT:
                    // 联系人
                    paasFormDataEntityExt = contactModel.getByKey(dataId, corpid);
                    break;
                case CUSTOMER_COMMUNICATE:
                case DISTRIBUTOR_COMMUNICATE:
                    // 跟进记录
                    paasFormDataEntityExt = customerCommunicateModel.getByKey(dataId, corpid);
                    break;
                case COMMUNICATE_PLAN:
                    // f访客计划
                    paasFormDataEntityExt = communicatePlanModel.getByKey(dataId, corpid);
                    break;
                case SALES_OPPORTUNITY:
                    // 机会
                    paasFormDataEntityExt = opportunityModel.getByKey(dataId, corpid);
                    break;
                case CONTRACT:
                case ORDER:
                    // 合同 订货单
                    paasFormDataEntityExt = contractModel.getByKey(dataId, corpid);
                    break;
                case RETURN:
                case REFUND:
                    paasFormDataEntityExt = refundModel.getByKey(dataId, corpid);
                    break;
                case WORK_REPORT:
                case WORKREPORT_WEEKLY:
                case WORKREPORT_DAILY:
                case WORKREPORT_MONTHLY:
                    paasFormDataEntityExt = workReportModel.getByKey(dataId, corpid);
                    break;
                case QUOTATION:
                    paasFormDataEntityExt = quotationModel.getByKey(dataId, corpid);
                    break;
                    // 进销存
                case SUPPLIER:
                    paasFormDataEntityExt = supplierModel.getByKey(dataId, corpid);
                    break;
                case SUPPLIER_CONTACT:
                    paasFormDataEntityExt = supplierContactModel.getByKey(dataId, corpid);
                    break;
                case SUPPLIER_COMMUNICATE:
                    paasFormDataEntityExt = supplierCommunicateModel.getByKey(dataId, corpid);
                    break;
                case PURCHASE:
                    paasFormDataEntityExt = purchaseModel.getByKey(dataId, corpid);
                    break;
                case RETURNED_PURCHASE:
                    paasFormDataEntityExt = returnedPurchaseModel.getByKey(dataId, corpid);
                    break;
                case BOM_BILL:
                    paasFormDataEntityExt = bomBillModel.getByKey(dataId, corpid);
                    break;
                case PRODUCTION_ORDER:
                    paasFormDataEntityExt = productionOrderModel.getByKey(dataId, corpid);
                    break;
                case ASSEMBLE:
                    paasFormDataEntityExt = assembleModel.getByKey(dataId, corpid);
                    break;
                case INSTOCK:
                case PURCHASE_INSTOCK:
                case OTHER_INSTOCK:
                case REFUND_INSTOCK:
                case RETURNED_MATERIEL_INSTOCK:
                case PRODUCTION_INSTOCK:
                    paasFormDataEntityExt = instockModel.getByKey(dataId, corpid);
                    break;
                case OUTSTOCK:
                case CONTRACT_OUTSTOCK:
                case OTHER_OUTSTOCK:
                case RETURNED_PURCHASE_OUTSTOCK:
                case PRODUCTION_MATERIEL_OUTSTOCK:
                case WORK_ORDER_OUTSTOCK:
                case ORDER_OUTSTOCK:
                    paasFormDataEntityExt = outstockModel.getByKey(dataId, corpid);
                    break;
                case INVENTORY:
                    paasFormDataEntityExt = inventoryModel.getByKey(dataId, corpid);
                    break;
                case TRANSFER:
                    paasFormDataEntityExt = transferModel.getByKey(dataId, corpid);
                    break;
                case WAREHOUSE:
                    paasFormDataEntityExt = warehouseModel.getByKey(dataId, corpid);
                    break;
                case PAYMENT:
                    paasFormDataEntityExt = paymentModel.getByKey(dataId, corpid);
                    break;
                case PAY_PLAN:
                    paasFormDataEntityExt = payPlanModel.getByKey(dataId, corpid);
                    break;
                case INVOICE:
                    paasFormDataEntityExt = invoiceModel.getByKey(dataId, corpid);
                    break;
                case WORK_ORDER:
                    paasFormDataEntityExt = workOrderModel.getByKey(dataId, corpid);
                    break;
                case PAYMENT_SHEET:
                    paasFormDataEntityExt = paymentSheetModel.getByKey(dataId, corpid);
                    break;
                case PAY_SHEET:
                    paasFormDataEntityExt = payPlanSheetModel.getByKey(dataId, corpid);
                    break;
                case PURCHASE_INVOICE:
                    paasFormDataEntityExt = purchaseInvoiceModel.getByKey(dataId, corpid);
                    break;
                case PRODUCT:
                    paasFormDataEntityExt = productModel.getByKey(dataId, corpid);
                    break;
                case REIMBURSEMENT_FORM:
                    paasFormDataEntityExt = reimbursementFormModel.getByKey(dataId, corpid);
                    break;
                case EXPENSE:
                    paasFormDataEntityExt = expenseModel.getByKey(dataId, corpid);
                    break;
                case CLUE:
                    paasFormDataEntityExt = clueModel.getByKey(dataId, corpid);
                    break;
                case MARKET_ACTIVITY:
                    paasFormDataEntityExt = marketActivityModel.getByKey(dataId, corpid);
                    break;
                case PAYMENT_TASK:
                    paasFormDataEntityExt = paymentTaskModel.getByKey(dataId, corpid);
                    break;
                case CREDIT_LIMIT:
                    paasFormDataEntityExt = creditLimitModel.getByKey(dataId, corpid);
                    break;
                case CREDIT_FIXED_LIMIT:
                    paasFormDataEntityExt = creditFixedLimitModel.getByKey(dataId, corpid);
                    break;
                case CREDIT_TEMPORARY_LIMIT:
                    paasFormDataEntityExt = creditTemporaryLimitModel.getByKey(dataId, corpid);
                    break;
                case CREDIT_CARD_BILL:
                    paasFormDataEntityExt = creditCardBillModel.getByKey(dataId, corpid);
                    break;
                case CUSTOMER_STATEMENT_STATISTIC:
                    paasFormDataEntityExt = customerStatementStatisticsModel.getByKey(dataId, corpid);
                    break;
                case DISTRIBUTOR_ACCOUNT:
                    DistributorAccountEntity distributorAccountEntity = distributorAccountModel.getByKey(dataId, corpid);
                    CustomerEntityExt customerEntityExt = customerModel.getByKey(distributorAccountEntity.getDistributorId(), corpid);
                    PaasFormDataEntity paasFormDataEntity = distributorAccountTransferSubForm.transferEntityToFormData(distributorAccountEntity);
                    paasFormDataEntity.getData().put(DistributorAccountEnum.DISTRIBUTOR_NAME_LINK_TEXT.getAttr(), customerEntityExt.getData().getString(DistributorEnum.NAME.getAttr()));
                    paasFormDataEntityExt.setFlowStatus(FlowStatusEnum.NOT_NEED_APPROVAL.getType());
                    paasFormDataEntity.setAppId(customerEntityExt.getAppId());
                    BeanUtil.copyProperties(paasFormDataEntity, paasFormDataEntityExt, true);
                    break;
                case ONLINE_PAYMENT_FLOW:
                    OnlinePaymentFlowEntity onlinePaymentFlowEntity = onlinePaymentFlowModel.getByKey(dataId, corpid);
                    BeanUtil.copyProperties(onlinePaymentFlowEntity, paasFormDataEntityExt);
                    break;
                case FUND_TRANSFER:
                    paasFormDataEntityExt = fundTransferModel.getByKey(dataId, corpid);
                    break;
                case OTHER_INCOME:
                    paasFormDataEntityExt = otherIncomeModel.getByKey(dataId, corpid);
                    break;
                case OTHER_EXPENSE:
                    paasFormDataEntityExt = otherExpenseModel.getByKey(dataId, corpid);
                    break;
                case COST_ADJUST:
                    paasFormDataEntityExt = costAdjustModel.getByKey(dataId, corpid);
                    break;
                case COMPETITOR:
                    paasFormDataEntityExt = competitorModel.getByKey(dataId, corpid);
                default:
                    break;
            }
        } else {
            paasFormDataEntityExt = paasFormDataModel.getByKey(dataId, corpid);
        }
        if (Objects.isNull(paasFormDataEntityExt)) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_DATA_NOT_EXIST);
        }
        return paasFormDataEntityExt;
    }
}
